home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / CMPLTPAS / BUTTON.ASM < prev    next >
Assembly Source File  |  1988-08-01  |  5KB  |  110 lines

  1. ;===========================================================================
  2. ;
  3. ;    B U T T O N  -  Function to return the state of the joystick buttons
  4. ;
  5. ;===========================================================================
  6. ;
  7. ;     by Jeff Duntemann      12 February 1988
  8. ;
  9. ;     From: COMPLETE TURBO PASCAL 5.0  by Jeff Duntemann
  10. ;    Scott, Foresman & Co., Inc. 1988   ISBN 0-673-38355-5
  11. ;
  12. ; BUTTON is written to be called from Turbo Pascal V4.0 using the
  13. ; {$L}/EXTERNAL procedure convention.
  14. ;
  15. ; Declare the procedure itself as external using this declaration:
  16. ;
  17. ; {$L BUTTON}
  18. ; FUNCTION BUTTON(StickNumber,ButtonNumber : Integer) : Boolean;
  19. ;                                                       EXTERNAL;
  20. ;
  21. ; StickNumber specifies which joystick to read from, and ButtonNumber
  22. ; specifies which of the two buttons on that joystick to read.  If the
  23. ; specified button is down, BUTTON returns a Boolean value of TRUE.
  24. ;
  25. ; Yes, this is the long way 'round; assembly language is in no way required
  26. ; to read four bits from an ordinary 8088 I/O port.  BUTTON exists only as
  27. ; practice in creating assembly language external functions.
  28. ;
  29. ; The button information is obtained by reading I/O port $201.  The high
  30. ; four bits represent the state of the four buttons (two for each of the
  31. ; two possible joysticks) at the instant the port is read.  A LOW bit
  32. ; represents a button DOWN.  This is why the byte read from the port is
  33. ; inverted via NOT before the selected bit is tested.
  34. ;
  35. ; Here is a map of the button bits as returned by port $201:
  36. ;
  37. ;     |7 6 5 4 3 2 1 0|
  38. ;      | | | |
  39. ;      | | |  - - - - - - -> Button #1, joystick #1
  40. ;      | |  - - - - - - - -> Button #2, joystick #1
  41. ;      |  - - - - - - - - -> Button #1, joystick #2
  42. ;       - - - - - - - - - -> Button #2, joystick #2
  43. ;
  44. ; Remember that the return value from this function is passed to the runtime
  45. ; code in the AL register.
  46. ;
  47. ; To reassemble/relink BUTTON:
  48. ;-------------------------------------
  49. ; Assemble this file with MASM.  "C>MASM BUTTON;"
  50. ;
  51. ;
  52. ; This structure defines the layout of parameters on the stack:
  53. ;
  54.  
  55. ONSTACK   STRUC
  56. OLDBP     DW   ?               ;TOP OF STACK
  57. RETADDR   DD   ?               ;FAR RETURN ADDRESS
  58. BTN_NO    DW   ?               ;BUTTON NUMBER
  59. STIK_NO   DW   ?               ;STICK NUMBER
  60. ONSTACK   ENDS
  61.  
  62. CODE    SEGMENT BYTE PUBLIC
  63.         ASSUME  CS:CODE
  64.         PUBLIC  BUTTON
  65.  
  66. BUTTON  PROC    FAR
  67.         PUSH    BP              ;SAVE PREVIOUS VALUE OF BP ON STACK
  68.         MOV     BP,SP           ;SP BECOMES NEW VALUE OF BP
  69.  
  70. ;-------------------------------------------------------------------
  71. ; THE BULK OF THIS ROUTINE SETS UP A TEST MASK BY WHICH ONE SINGLE
  72. ; BIT OUT OF THE FOUR BUTTON BITS IS TESTED.
  73. ;-------------------------------------------------------------------
  74.  
  75.         MOV     BL,010H         ;START WITH HIGH BIT IN BIT 4
  76.         CMP     [BP].STIK_NO,2  ;ARE WE TESTING FOR JOYSTICK #2?
  77.         JNE     WHICH           ;IF NOT, GO ON TO TEST FOR WHICH BUTTON,
  78.         SHL     BL,1            ; OTHERWISE SHIFT TWO POSITIONS LEFTWARD
  79.         SHL     BL,1            ; SO THAT THE MASK IS ON BIT 6 FOR STICK 2
  80.  
  81. WHICH:  CMP     [BP].BTN_NO,2   ;ARE WE TESTING FOR BUTTON #2?
  82.         JNE     READEM          ;IF NOT, MASK IS CORRECT; GO READ PORT
  83.         SHL     BL,1            ;OTHERWISE, SHIFT 1 BIT LEFT FOR BUTTON 2
  84.  
  85. ;------------------------------------------------------------------------
  86. ; THE BIT MASK IS NOW CORRECT.  HERE THE BUTTON BITS ARE READ FROM PORT
  87. ; $201 AND TESTED AGAINST THE MASK.  NOTE THAT THE BITS AS READ FROM
  88. ; THE PORT MUST BE INVERTED SO THAT THE Z FLAG IS SET RATHER THAN CLEARED
  89. ; ON AN ACTIVE BUTTON BIT.  (BITS ARE ACTIVE **LOW**, REMEMBER!)
  90. ;------------------------------------------------------------------------
  91.  
  92. READEM: MOV     DX,0201H        ;SET UP 16-BIT ADDRESS FOR PORT READ
  93.         IN      AL,DX           ;READ BUTTON BITS FROM PORT $201
  94.         NOT     AL              ;MUST INVERT BITS FOR PROPER SENSE
  95.                                 ; OF THE Z FLAG AFTER TESTING
  96.         TEST    AL,BL           ;SEE IF THE DESIRED BIT IS HIGH;
  97.         JNZ     PUSHED          ;IF SO, BUTTON IS PUSHED;
  98.         MOV     AL,0            ;ELSE MOVE BOOLEAN FALSE INTO AL
  99.         JMP     DONE            ;AND GET OUT OF HERE
  100.  
  101. PUSHED: MOV     AL,1            ;BUTTON DOWN; MOVE BOOLEAN TRUE INTO AL
  102.  
  103. DONE:   MOV     SP,BP           ;RESTORE PRIOR STACK POINTER & BP
  104.         POP     BP              ; IN CONVENTIONAL RETURN
  105.         RET     6
  106.  
  107. BUTTON  ENDP
  108. CODE    ENDS
  109.         END
  110.